package com.zhuannimei.ui;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.util.LogUtils;
import com.zhuannimei.GlobalApplication;
import com.zhuannimei.R;
import com.zhuannimei.adapter.BuyHistoryAdapter;
import com.zhuannimei.adapter.HistoryOneGouAdapter;
import com.zhuannimei.adapter.ProductHistoryAdapter;
import com.zhuannimei.domain.HistoryOneShop;
import com.zhuannimei.domain.ProductHistory;
import com.zhuannimei.net.RequestServerData;
import com.zhuannimei.parser.HistoryOneShopParser;
import com.zhuannimei.parser.ProductHistoryParser;
import com.zhuannimei.service.AsyncDataLoader;
import com.zhuannimei.service.AsyncDataLoader.Callback;
import com.zhuannimei.utility.WuLog;
import com.zhuannimei.utility.ZnmData;
import com.zhuannimei.utility.ZnmHttpParams;
import com.zhuannimei.view.ZnmRefreshListView;
import com.zhuannimei.view.ZnmRefreshListView.OnCancelListener;
import com.zhuannimei.view.ZnmRefreshListView.OnRefreshListener;

/**
 * 兑换购买记录
 * 
 * @author Jack 2014-3-5
 * 
 * 
 */
public class DuiHuanBuyHistoryActivity extends BaseActivity {
	private static final String TAG = "DuiHuanBuyHistoryActivity";
	private ProductHistoryAdapter mFirstAdapter;
	private ZnmRefreshListView duihuan_ZR;
	private List<ProductHistory> mFirstList = new ArrayList<ProductHistory>();

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.duihuan_buy_history);
		initView();
		setListener();
		setData();
	}

	/**
	 * 
	 */
	private void getBuyHistory() {
		RequestParams mParams = ZnmHttpParams.getDefaultRequestParams();
		mParams.addBodyParameter("pageIndex", "0");
		mParams.addBodyParameter("userId", ZnmData.getUserData()
				.getUserUserId());
		RequestServerData.getProductHistory(mParams,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						WuLog.i(TAG, "" + responseInfo.result);
						try {
							JSONObject jo = new JSONObject(responseInfo.result);
							if ("0".equals(jo.optString("code"))) {
								List<ProductHistory> list = new ProductHistoryParser()
										.parseJSON(responseInfo.result);
								if (list != null && list.size() != 0) {
									mFirstList.clear();
									mFirstList.addAll(list);
								}
								mFirstAdapter.notifyDataSetChanged();
								// 先删后存
								GlobalApplication.appDB.deleteByWhere(
										ProductHistory.class, null);
								for (ProductHistory pu : mFirstList) {
									GlobalApplication.appDB.save(pu);
								}
							}
							duihuan_ZR.onRefreshComplete();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						showCustomToast("网络不给力" + error.getExceptionCode());
					}
				});
	}

	@Override
	protected void onStart() {
		super.onStart();
		mTitlePane.getLeftButton().setVisibility(View.VISIBLE);
		mTitlePane.setTitle("积分商城购买记录");
		mTitlePane.setLeftButtonTextAndListener("返回", new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void setListener() {
		duihuan_ZR.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				getBuyHistory();
			}
		});
		duihuan_ZR.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel() {
				duihuan_ZR.onRefreshComplete();
			}
		});
	}

	private void setData() {
		new AsyncDataLoader(new Callback() {
			@Override
			public void onStart() {
				// 查询
				mFirstList.clear();
				try {
					List<ProductHistory> flist = GlobalApplication.appDB
							.findAll(ProductHistory.class);
					if (flist != null && flist.size() != 0) {
						mFirstList.addAll(flist);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onPrepare() {
				// 处于刷新状态
				mFirstAdapter = new ProductHistoryAdapter(
						getApplicationContext(), mFirstList);
				duihuan_ZR.setAdapter(mFirstAdapter);
				duihuan_ZR.onManualRefresh();
			}

			@Override
			public void onFinish() {
				getBuyHistory();
			}
		}).execute();
	}

	private void initView() {
		duihuan_ZR = (ZnmRefreshListView) findViewById(R.id.duihuan_ZR);
	}
}
